<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <!-- import CSS -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/element-ui/2.15.9/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <input type="text" placeholder="员工姓名" v-model="emp.name">
    <input type="text" placeholder="员工工资" v-model="emp.sal">
    <input type="text" placeholder="员工工作"v-model="emp.job">
    <br>
    <input type="button" value="添加" @click="f()">
    <template>
        <el-table :data="arr" style="width: 100%">
            <el-table-column type="index" label="编号">
            </el-table-column>
            <el-table-column prop="name" label="姓名" >
            </el-table-column>
            <el-table-column prop="sal" label="工资" >
            </el-table-column>
            <el-table-column prop="job" label="工作">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </template>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.staticfile.org/element-ui/2.15.9/index.min.js"></script>
<script>
    let v = new Vue({
        el: '#app',
        data: function () {
            return{
                emp:{name:"",sal:"",job:""},
                arr:[
                    {name: '张三', sal: 2000, job: '公务员'},
                    {name: '李四', sal: 1000, job: '技术员'},
                    {name: '王五', sal: 3000, job: '销售'},
                    {name: '赵六', sal: 4000, job: 'CEO'}
                ]

            }
        },
        methods: {
            f(){
                //创建一个新的对象，数据来自和页面进行绑定的v.emp对象，把对象数据添加到数组中
                //由于页面中的表格和数组进行了绑定 数组里面的数据改变 页面也会自动改变
                v.arr.push({name:v.emp.name,sal:v.emp.sal,job:v.emp.job});
            },
            handleDelete(i, emp) {
                if (confirm("您确认删除吗？")){
                    //从数组中删除元素
                    v.arr.splice(i,1)
                    v.$message("成功删除了"+emp.name)
                }
            }
        }
    })
</script>
</html>